var api = require("../../../api.js"),
  utils = require("../../../utils.js"),
  app = getApp();

Page({
  data: {
    groupFail: 0,
    show_attr_picker: !1,
    form: {
      number: 1
    },
    id: "",
    cat_id: "",
    user_info: {},
    goods: {},
    cutprice: "",
    user_id: "",
    opencut1: false,
    userlistindex: 0,
    jia: [],
    show: true,
    width: null,
  },
  onLoad: function(options) {
    var rule = ["1.啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦",
      "2.啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦",
      "3.啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦",
      "4.啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦",
      "5.啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦",
    ];
    this.setData({
      rule: rule,
        winH: wx.getSystemInfoSync().windowHeight
    })
    app.pageOnLoad(this), this.loadData(options);
  },

  loadData: function(options) {
    var that = this;
    wx.showShareMenu({
      withShareTicket: true
    })
    //接收上个跳转页面的参数
    console.log('onload', options)
    this.setData({
      id: options.gid,
      cat_id: options.cat_id
    })
    if (options.who == 0) {
      var who = 0;
    } else {
      var who = 1;
    }
    this.setData({
      who: options.who,
    })

    //发起砍价后，系统自动帮忙砍一刀
    // debugger;
    if (who == 0) {
      //个人信息
      app.request({
        url: api.user.index,
        method: "get",
        data: {},
        success: function(res) {
          console.log("user_info")
          console.log(res)
          that.setData({
            user_info: res.data.user_info,
            user_id: res.data.user_info.id
          });
        }
      });

      console.log("gid===============>", options.gid)
      console.log("cat_id===============>", options.cat_id)
      console.log(this)
      app.request({
        url: api.kanjia.order.system_bargain,
        method: "get",
        data: {
          goods_id: options.gid,
          cat_id: options.cat_id
        },
        success: function(res) {
          console.log("系统砍一刀")
          console.log("bid=============>", res.data)
          res.data == undefined && wx.showToast({
            title: '活动时间已结束',
          })
          that.setData({
            bid: res.data.bid
          })
          if (res.code == 0) {
            that.setData({
              opencutsys: true,
            })
          } else {
            console.log("else")
            var msg = res.msg;
            wx.showToast({
              title: msg,
              icon: "none",
              duration: 2000
            })
          }
          //系统以及好友帮我砍下金额列表
          app.request({
            url: api.kanjia.order.jia_tou,
            method: "get",
            data: {
              bid: res.data.bid,
            },
            success: function(res) {
              console.log('jia_tou============>', res)
              let a = res.data,
                c = res.data.jia_record
              that.setData({
                jia_tou: a['jia_record'],
              })
              var first = 0;
              for (var key in c) {
                first += Math.floor(parseFloat(c[key].bargainprice) * 100) / 100
              }
              that.setData({
                first: first
              })
              console.log(first)

              //物品信息
              app.request({
                url: api.kanjia.list,
                method: "get",
                data: {
                  gid: options.gid,
                },
                success: function(res) {
                  var goods = res.data.list[0];
                  that.setData({
                    goods: goods,
                    original_price: goods.original_price
                  })
                  res.data.list[0].original_price - first -1000>= res.data.list[0].price || that.setData({
                    show: false
                  })
                  console.log(goods.original_price)
                  let jdt = (first / goods.original_price) * 100
                  console.log('who==============0',jdt)
                  that.setData({
                    width: jdt
                  })
                }
              });

            }
          });
        }
      });

    } else {
      console.log("else")
      console.log(options)
      //bid 用户信息 api
      app.request({
        url: api.kanjia.jia_tuser,
        method: "get",
        data: {
          gid: options.gid,
          user_id: options.user_id
        },
        success: function(res) {
          console.log("jia_tuser")
          console.log("bid=============>", res.data)
          that.setData({
            bid: res.data.bid,
            user_info: res.data.user_info,
            user_id: res.data.user_id
          })

          //系统以及好友帮我砍下金额列表
          console.log("jia_tou的bid")
          console.log(res.data.bid)
          app.request({
            url: api.kanjia.order.jia_tou,
            method: "get",
            data: {
              bid: res.data.bid,
            },

            success: function(res) {
              console.log('jia_tou============>', res)
              let a = res.data,
                c = res.data.jia_record
              that.setData({
                jia_tou: a['jia_record'],
              })
              var first = 0;
              for (var key in c) {
                first += Math.floor(parseFloat(c[key].bargainprice) * 100) / 100
              }
              that.setData({
                first: first,
              })
              console.log(first)

              //物品信息
              app.request({
                url: api.kanjia.list,
                method: "get",
                data: {
                  gid: options.gid,
                },
                success: function(res) {
                  var goods = res.data.list[0];
                  that.setData({
                    goods: goods,
                    original_price: goods.original_price
                  })
                  console.log("goods" + goods)
                  let jdt = (first / goods.original_price) * 100
                  console.log(jdt)
                  that.setData({
                    width: jdt
                  })
                  console.log('who=1=============>',jdt)
                },
              });
            },

          });
        }
      });
    }
  },
 
  cut1: function(options) {
    let that = this
    console.log("cut1<===========")
    console.log(options)
    app.request({
      url: api.kanjia.order.help_bargain,
      method: "get",
      data: {
        bid: options.currentTarget.dataset.bid,
        goods_id: options.currentTarget.dataset.goods_id,
        cat_id: options.currentTarget.dataset.cat_id,
        user_id: options.currentTarget.dataset.user_id   
      },
      success: function(res) {
        console.log("========success=cut1==========")
        console.log(res)
        if (res.code == 0) {
          that.setData({
            opencut1: true,
          })
          var cutprice = res.data;
          that.setData({
            cutprice: cutprice,
            bargainprice: res.data.bargainprice
          })
        } else {
          var msg = res.msg;
          wx.showToast({
            title: msg,
            icon: "none",
            duration: 2000
          })
          setTimeout(function() {
            wx.redirectTo({
              url: "/pages/kj/index/index"
            });
          }, 2000);
        }
        
        let jdt = (1 - (res.data.leftPrice / options.currentTarget.dataset.price)) * 100
        console.log('jdt==============>',jdt)
        that.setData({
          width: jdt
        })
      }
    });
  },
  cover: function(){
    let that = this
    that.setData({
      opencut1: false,
    })
  },


  // 不砍了 规则
  cancel: function() {
    this.setData({
      open: true,
      isScroll: false,
    })
  },

  // 不砍了
  end: function(t) {
    console.log(t.currentTarget.dataset.bid)
    app.request({
      url: api.kanjia.order.jia_tchop,
      method: "get",
      data: {
        bid: t.currentTarget.dataset.bid
      },
      success: function(res) {
        console.log(res)
      }
    });
    wx.redirectTo({
      url: '../index/index',
    })
  },

  // 继续砍
  close: function(res) {
    console.log(res)
    this.setData({
      open: false,
      opencut: false,
      isScroll: true,
      opencutsys: false,
    })
  },

  // 购买
  cutbuy: function(res) {
    console.log("click")
    console.log(res.currentTarget.dataset.bid)
    wx.redirectTo({
      url: '/pages/kj/details/details?gid=' + res.currentTarget.dataset.id + '&buy=' + true + '&bid=' + res.currentTarget.dataset.bid
    })
  },

  // 喊好友砍一刀
  cut0: function(options) {
    this.setData({
      opencut: true,
    })
  },

  // 查看所有砍价商品>
  opencutbutton: function() {
    wx.navigateTo({
      url: '../index/index',
    })
  },

  // 好友帮砍榜 请TA来帮我
  userlist: function(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      userlistindex: index,
    })
  },

  // 我的砍价历史
  history: function() {
    wx.navigateTo({
      url: '/pages/kj/history/history?cat_id=' + this.options.cat_id + '&id=' + this.data.goods.id + '&gid=' + this.options.gid,
    })
    console.log("===history===")
    console.log(this)
  },
  onReady: function() {},
  onShow: function() {
    app.pageOnShow(this);
  },
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},

  //转发设置
  onShareAppMessage: function() {
    console.log("onshareMess")
    var t = this,
      a = wx.getStorageSync("user_info"),
      e = "/pages/kj/knife/knife?who=1" + '&gid=' + t.data.goods.id + "&user_id=" + a.id + '&cat_id=' + t.data.goods.cat_id;
    console.log(t)
    return {
      title: t.data.goods.name,
      path: e,
      imageUrl: t.data.goods.cover_pic,
      success: function(t) {
        console.log(e);
      }
    };
  },

});